<?php
session_start();
?>

<html>
	<head>
		<script type= "text/javascript" src= "Photo_Scrape.js" ></script>
		<script type= "text/javascript" src= "../index.js" ></script>
		<link rel="stylesheet" href="photoScrape.css">
	</head>
<body>

<div id="googleMap"></div>

<?php

$target_dir = "../uploads//";
$target_file = $target_dir . basename($_FILES["fileName"]["name"]);

$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

//echo "$target_file <br>";

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } 
  else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

//Check for if the file already exists
if(file_exists($target_file)){
	echo "File already exists, try a different file";
	$uploadOk = 0;
}

//Allow only certain file types //MOVE THIS OVER TO JS
//Allowed types: JPEG, JPG, TIFF, others I have yet to decide
if($imageFileType != "jpeg" && $imageFileType != "jpg"){
	echo "Wrong File Type. <br />";
	$uploadOk = 0;
}

//We don't check for file size as we handle that in Photo_Scrape.js

///////////////////////////////////////////////////////////////////////
//Below is not needed for final version.
//if (!empty($_FILES))
//{
//    print_r($_FILES);  // displays the contents of associative array, just to see it
//}
//Above is not needed for final version.
///////////////////////////////////////////////////////////////////////

//Check to see if an error code was generated on the upload attempt
  if ($_FILES['fileName']['error'] > 0)
  {
    $uploadOk = 0;
	echo 'Problem: ';
    switch ($_FILES['fileName']['error'])
    {
      case 1:	echo 'File exceeded upload_max_filesize';
	  			break;
      case 2:	echo 'File exceeded max_file_size';
	  			break;
      case 3:	echo 'File only partially uploaded';
	  			break;
      case 4:	echo 'No file uploaded';
	  			break;
	  case 6:   echo 'Cannot upload file: No temp directory specified.';
	  			break;
	  case 7:   echo 'Upload failed: Cannot write to disk.';
	  			break;
    }
    exit;
  }
  
//Check if any of our checks have 'failed'  
if($uploadOk != 0){
	if (!empty($_FILES)){
		// Displays the uploaded image. Testing purposes 			//WORKS//
		$name = "../uploads//".$_FILES['fileName']['name'];
		if ( !move_uploaded_file($_FILES['fileName']['tmp_name'], $name) ){
			echo "<div id='uploadContainer'>";
				echo "unable to move the file";
				echo "<a href='../liveIndex.php'>Return</a>";
			echo "</div>";
		}
		else{
			echo "<div id='uploadContainer'>";
				echo "<center><img src='$name'/><br> </center> <br>";  // displays the image 
		}

		// Read the meta data from a photo (Jpeg/TIFF) and potentially return arrays of data.
		// Source: https://www.php.net/manual/en/function.exif-read-data.php
		$exif = exif_read_data("../uploads//".$_FILES['fileName']['name'], 0, true);
	
		//create the variable to save to the session
		$meta_data = "";
		
		echo "<form onsubmit= 'return verifyData(this)' id= 'photoScrape' name= 'photoScrape' method = 'post' action= 'combineImg.php'>";
	
		/*foreach ($exif as $key => $section){
			foreach ($section as $pname => $val){
				echo "$key.$pname: $val <br>";
				if(trim($key) == "FILE" && trim($pname) == "FileName")
					echo "hello <input type= 'text' name= 'fileName' id= 'fileName' value= '$val'></input>";
			}
		}*/
		if($val = $exif['FILE']['FileName']){
			echo "<center> File Name <input type= 'text' name= 'fileName' id= 'fileName' value= '$val' readonly></input> </center> <br>";
			
			//$meta_data = $meta_data.$val.';';
		}
		
		if($val = $exif['EXIF']['DateTimeOriginal']){
			$two = explode(' ', $val);
			$theDate = $two[0];
			$theTime = $two[1];
			echo "<center> Date <input type= 'text' name= 'theDate' id= 'theDate' value= '$theDate' readonly></input> </center> <br>";
			echo "<center> Time <input type= 'text' name= 'theTime' id= 'theTime' value= '$theTime' readonly></input> </center> <br>";
			
			//$meta_data = $meta_data.$theDate.';';
			//$meta_data = $meta_data.$theTime.';';
		}
		else{
			echo "<center> Date <input type= 'text' name= 'theDate' id= 'theDate' value= '' ></input> </center> <br>";
			echo "<center> Time <input type= 'text' name= 'theTime' id= 'theTime' value= '' ></input> </center> <br>";
		}
		
		if($val = $exif['GPS']['GPSLatitude']){
			$degrees = explode('/', $val[0])[0] / explode('/', $val[0])[1];
			$minutes = explode('/', $val[1])[0] / explode('/', $val[1])[1];
			$seconds = explode('/', $val[2])[0] / explode('/', $val[2])[1];
			$result = $degrees + $minutes/60 + $seconds/3600;
			if($exif['GPS']['GPSLatitudeRef'] == 'S') $result = -$result;
			echo " <center> Latitude <input type= 'text' name= 'theLat' id= 'theLat' value= '$result' readonly></input> </center> <br>";
			
			//$meta_data = $meta_data.$result.';';
		}
		else
			echo "<center> Latitude <input type= 'text' name= 'theLat' id= 'theLat' value= ''></input> </center> <br>";
			//Use the google maps autofill feature to fill this in using a city.
		
		if($val = $exif['GPS']['GPSLongitude']){
			$degrees = explode('/', $val[0])[0] / explode('/', $val[0])[1];
			$minutes = explode('/', $val[1])[0] / explode('/', $val[1])[1];
			$seconds = explode('/', $val[2])[0] / explode('/', $val[2])[1];
			$result = $degrees + $minutes/60 + $seconds/3600;
			if($exif['GPS']['GPSLongitudeRef'] == 'W') $result = -$result;
			echo "<center> Longitude <input type= 'text' name= 'theLong' id= 'theLong' value= '$result' readonly></input> </center> <br>";
			
			//$meta_data = $meta_data.$result.';';
		}
		else
			echo "<center> Longitude <input type= 'text' name= 'theLong' id= 'theLong' value= ''></input> </center> <br>";
		
		echo "<center> City <input type= 'text', name='theCity' id= 'theCity' value= '' ></input> </center> <br>";
		
		echo "<center> <label> <input type= 'radio' name= 'view' id= 'private' value= 'private' /> Private </label> </center>";
		echo "<center> <label> <input type= 'radio' name= 'view' id= 'public' value= 'public' checked = 'checked' /> Public </label> </center> <br>";
		echo "<p />";
	
		echo "<center> <input type='submit' value='Upload' /> </center>";
		echo "</form>";
		echo "</div>";
		
		//Data needed for my project (either from the metadata or manually inputted): fileName, lat/long, date, city
		//echo "$meta_data <br>";
		$_SESSION['meta_data'] = $meta_data;
		//echo $_SESSION['meta_data'];
	}
}

?>

<!-- If the user decides to not complete the upload then delete the file from the uploads folder-->
 <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAkAZn5BQC3mdFoHCgnalLZGs7BHtQDyoc&callback=myEmptyMap"></script>

</body>
</html>